package activity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.view.MotionEvent;
import android.widget.TextView;

import com.jakewharton.rxbinding.view.RxView;
import com.jzxiang.pickerview.TimePickerDialog;
import com.jzxiang.pickerview.listener.OnDateSetListener;

import net.ck.hzq.R;
import net.ck.hzq.TobuyApplication;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

import Bean.JiFenRecordBean;
import base.BaseAct;
import base.BaseBiz;
import butterknife.BindView;
import cn.finalteam.loadingviewfinal.OnDefaultRefreshListener;
import cn.finalteam.loadingviewfinal.PtrClassicFrameLayout;
import cn.finalteam.loadingviewfinal.PtrFrameLayout;
import cn.finalteam.loadingviewfinal.RecyclerViewFinal;
import event.NetErrorEvent;
import ui.activity.login.biz.LoginBiz;
import ui.adapter.JiFenLiuZhuanRecordAdapter;
import ui.adapter.JiFenRecordAdapter;
import util.AlertUtils;
import util.SysUtils;


/**
 * 提现列表
 * Created by chenjian on 2017/8/4.
 */

public class LiuZhuanRecordAct extends BaseAct{


    @BindView(R.id.profit_day)
    TextView day;
    @BindView(R.id.profit_check)
    TextView check;
    @BindView(R.id.pull_refresh_rv)
    RecyclerViewFinal rv;
    @BindView(R.id.pull_refresh_ptr)
    PtrClassicFrameLayout ptr;
    String year="";
    String month="";
    private SharedPreferences sp;

    JiFenLiuZhuanRecordAdapter adp;
    String timeStr = "";
    List<JiFenRecordBean> itemsBeans = new ArrayList<>();

    long selectTime;

    @Override
    protected void setRoot() {
//        setTitleBar(R.color.colorBlue);
        setContentView(R.layout.activity_presentrecord);
    }

    @Override
    protected void setup() {

        setToolBar("流转记录", "");

    }


    @Override
    protected void init(@Nullable Bundle savedInstanceState) {
        selectTime = System.currentTimeMillis();
        try {
            String str = SysUtils.sf2.format(new Date(System.currentTimeMillis())).replace("-", "年")
                    + getResources().getString(R.string.lang_mouth);
            day.setText(str);
            timeStr = SysUtils.sf2.format(new Date(System.currentTimeMillis()));
        } catch (Exception e) {

        }
        rv.setLayoutManager(new LinearLayoutManager(LiuZhuanRecordAct.this));
        rv.setItemAnimator(new DefaultItemAnimator());
//        rv.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
        ptr.setLastUpdateTimeRelateObject(this);
        ptr.disableWhenHorizontalMove(true);
//        rv.setOnLoadMoreListener(new OnLoadMoreListener() {
//            @Override
//            public void loadMore() {
//            }
//        });
        ptr.setOnRefreshListener(new OnDefaultRefreshListener() {
            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
//                presenter.getList(true, sp.getString("id", "0"));
                getData(year,month);
            }
        });
        RxView.touches(day)
                .compose(this.<MotionEvent>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(MotionEvent -> {
                    TimePickerDialog timePickerDialog = AlertUtils.timePicker3(LiuZhuanRecordAct.this, 365 * 5, 0,selectTime, new OnDateSetListener() {
                        @Override
                        public void onDateSet(TimePickerDialog timePickerView, long millseconds) {
                            selectTime=millseconds;
                            String str = SysUtils.sf2.format(new Date(millseconds)).replace("-", "年")
                                    + getResources().getString(R.string.lang_mouth);
                            year = SysUtils.sf7.format(new Date(millseconds));
                            month = SysUtils.sf8.format(new Date(millseconds));
                            timeStr = SysUtils.sf2.format(new Date(millseconds));
                            day.setText(str);
                            getData(year,month);
                        }
                    });
                    timePickerDialog.show(LiuZhuanRecordAct.this.getSupportFragmentManager(), "all");
                });

        RxView.clicks(check)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(Void -> getData(year,month));
        ptr.autoRefresh(true);

        year = SysUtils.sf7.format(new Date(System.currentTimeMillis()));
        month = SysUtils.sf8.format(new Date(System.currentTimeMillis()));
        getData(year,month);

    }


    @Override
    public void onNetErrorEvent(NetErrorEvent event) {
        super.onNetErrorEvent(event);
    }

    @Override
    protected void onNetError(NetErrorEvent event) {

    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }


    public void refreshComplete() {
        ptr.onRefreshComplete();
    }


    private void getData(String year,String month){
        new LoginBiz().jifenrecord(year,month,"0", new BaseBiz.Callback<JiFenRecordBean>() {
            @Override
            public void onSuccess(JiFenRecordBean integralTotalBean) {
                ptr.onRefreshComplete();
                itemsBeans.clear();
                itemsBeans.add(integralTotalBean);
                if (adp == null) {
                    adp = new JiFenLiuZhuanRecordAdapter(LiuZhuanRecordAct.this, itemsBeans,0);
                    rv.setAdapter(adp);
                } else {
                    adp.notifyDataSetChanged();
                }

            }

            @Override
            public void onFailure(JiFenRecordBean integralTotalBean) {

            }
        });


    }


    private void setInfo() {
//        if (itemsBeans.size() == 0) {
//            showMsg("您暂时没有提现记录");
//            return;
//        }
//        if (adp == null) {
//            adp = new PresentRecordAdapter(GetJIFenRecordAct.this, beans);
//            rv.setAdapter(adp);
//        } else {
//            adp.getData().clear();
//            adp.setDatas(beans);
//            adp.notifyDataSetChanged();
//        }

    }

}
